package com.lyn.plugin

import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.lifecycleScope
import com.lyn.pluginapi.api.PluginApi
import com.lyn.pluginapi.factory.ClassLoaderFactory
import com.lyn.pluginapi.factory.ResourcesFactory
import com.lyn.pluginapi.jump.JumpHelper

/**
 * @author longyn 2025/7/4
 * @version 1.0.0
 */
open class Plugin : PluginApi {

    lateinit var pluginId: String
    lateinit var activity: AppCompatActivity
    override fun onProxyActivity(pluginId: String, activity: AppCompatActivity) {
        this.pluginId = pluginId
        this.activity = activity
    }

    override fun onCreate(savedInstanceState: Bundle?) {

    }

    override fun onStart() {

    }

    override fun onResume() {

    }

    override fun onPause() {

    }

    override fun onStop() {

    }

    override fun onDestroy() {

    }

    override fun getResources(): Resources {
        return ResourcesFactory.get(pluginId)!!
    }

    override fun getClassLoader(): ClassLoader {
        return ClassLoaderFactory.get(pluginId)!!
    }

    override fun getString(resId: Int): String {
        return activity.getString(resId)
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun getDrawable(resId: Int): Drawable? {
        return activity.getDrawable(resId)
    }

    override fun getLayoutInflater(): LayoutInflater {
        return activity.layoutInflater
    }

    override fun setContentView(layoutResID: Int) {
        activity.setContentView(layoutResID)
    }

    override fun setContentView(view: View) {
        activity.setContentView(view)
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        return false
    }

    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        return false
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return false
    }

    override fun onKeyLongPress(keyCode: Int, event: KeyEvent?): Boolean {
        return false
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        return false
    }

    override fun isHandleOnBackPressedEnabled(): Boolean {
        return false
    }

    override fun onHandleOnBackPressed() {

    }

    override fun showToast(message: String, duration: Int) {
        Toast.makeText(activity, message, duration).show()
    }

    override fun onActivityResultCallback(activityResult: ActivityResult) {

    }

    override fun starActivity(clazz: Class<*>) {
        JumpHelper.instance.springboardPluginId = pluginId
        JumpHelper.instance.springboardPluginClassName = clazz::class.java.name
        activity.startActivity(Intent(activity, clazz))
    }

    override fun finish() {
        activity.finish()
    }
    
    override fun getLifecycle():Lifecycle{
        return activity.lifecycle
    }

    override fun getLifecycleScope(): LifecycleCoroutineScope {
        return activity.lifecycleScope
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return false
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        return false
    }

    override fun getIntent(): Intent {
        return activity.intent
    }

    override fun onNewIntent(intent: Intent?) {

    }

}